#include "CIpodList.h"

std::list<CIpodDevice *> CIpodList::s_DeviceList;

CIpodDevice *CIpodList::getDevice(ipod_t handle) {
    CIpodDevice *device = NULL;
    for (std::list<CIpodDevice *>::iterator iter = s_DeviceList.begin(); iter != s_DeviceList.end(); ++iter) {
        device = *iter;
        if (handle == device->getHandle()) {
            break;
        }
    }
    return device;
}

void CIpodList::push(CIpodDevice *device) {
    if (device == NULL) {
        return;
    }
    s_DeviceList.push_back(device);
}

void CIpodList::pop(ipod_t handle) {
    for (std::list<CIpodDevice *>::iterator iter = s_DeviceList.begin(); iter != s_DeviceList.end(); ) {
        CIpodDevice *device = *iter;
        if (device->getHandle() == handle) {
            device->destroy();
            s_DeviceList.erase(iter);
            delete device;
            break;
        } else {
            ++iter;
        }
    }
}

bool CIpodList::isDeviceExist(ipod_t handle) {
    bool flag = false;
    for (std::list<CIpodDevice *>::iterator iter = s_DeviceList.begin(); iter != s_DeviceList.end(); ++iter) {
        if (handle == (*iter)->getHandle()) {
            flag = true;
            break;
        }
    }
    return flag;
}

bool CIpodList::isDeviceExist(const char *path) {
    std::string devicePath;
    if (path != NULL) {
        devicePath = path;
    }
    bool flag = false;
    for (std::list<CIpodDevice *>::iterator iter = s_DeviceList.begin(); iter != s_DeviceList.end(); ++iter) {
        if (devicePath.compare((*iter)->getPath()) == 0) {
            flag = true;
            break;
        }
    }
    return flag;
}

std::list<CIpodDevice *> CIpodList::getDeviceList() {
    return s_DeviceList;
}